package com.inscream.app.profileTab;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.inscream.app.JSONParser;
import com.inscream.app.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class MyFavorites extends Activity {


    ListView myOwnPost;
    TextView recordName;
    TextView recordDescription;
    TextView recordTag;
    TextView getReSoundScore, getLikeScore;
    TextView hiddenId;
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
    MediaPlayer playerMy = new MediaPlayer();
    Boolean suandaCaliyorsa = false;
    private static String url = "http://mehmetalp.com.tr/seslitwitter/homepage.php?method=3&userId=2";
    private static final String TAG_OS = "posts";
    private static final String soundRecordName = "soundRecordName";
    private static final String soundRecordDescription	 = "soundRecordDescription";
    private static final String soundRecordTag = "soundRecordTag";
    private static final String reSoundScore = "reSoundScore";
    private static final String likeSoundScore = "likeSoundScore";
    private static final String id = "id";
    JSONArray android = null;

    private boolean isRunning;
    public RelativeLayout mybottombar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_favorites);


        new Handler().postDelayed(new Runnable() {
            public void run() {
                new JSONParse().execute();
            }
        }, 100);

        mybottombar = (RelativeLayout) findViewById(R.id.mybottombar);
        playerMy.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
            @Override
            public void onPrepared(MediaPlayer mp1) {
                // TODO
                playerMy.start();
            }
        });

        playerMy.setScreenOnWhilePlaying(true);
        playerMy.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                suandaCaliyorsa = false;
                mp.reset();
            }
        });

    }

    ///////////////////////////////////////////////

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            recordName = (TextView)findViewById(R.id.vers);
            recordDescription = (TextView)findViewById(R.id.name);
            recordTag = (TextView)findViewById(R.id.api);
            hiddenId = (TextView)findViewById(R.id.hiddenId);
            getLikeScore = (TextView)findViewById(R.id.getLikeScore);
            getReSoundScore = (TextView)findViewById(R.id.getReSoundScore);
           // pDialog = new ProgressDialog(MyFavorites.this);
            //  pDialog.setMessage("Wait a min ...");
            //  pDialog.setIndeterminate(false);
            //  pDialog.setCancelable(true);
            //   pDialog.show();
        }

      @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();
            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }


        @Override
        protected void onPostExecute(JSONObject json) {
            //   pDialog.dismiss();
            try {
                // Getting JSON Array from URL

                android = json.getJSONArray(TAG_OS);
                for(int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);
                    // String  JSON item in a Variable
                    String recordName = c.getString(soundRecordName);
                    String recordDescription = c.getString(soundRecordDescription);
                    String recordTag = c.getString(soundRecordTag);
                    String recordId = c.getString(id);
                    String getLikeScore = c.getString(likeSoundScore);
                    String getReSoundScore = c.getString(reSoundScore);
                    // Adding value HashMap key => value
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(soundRecordName, recordName);
                    map.put(soundRecordDescription, recordDescription);
                    map.put(soundRecordTag, recordTag);
                    map.put(id, recordId);
                    map.put(likeSoundScore, getLikeScore);
                    map.put(reSoundScore, getReSoundScore);
                    oslist.add(map);
                    myOwnPost =(ListView)findViewById(R.id.favlistView);
                    // myOwnPost.setRotation(-90);
                    ListAdapter adapter = new SimpleAdapter(MyFavorites.this, oslist,
                            R.layout.homelistviewitem,
                            new String[] { soundRecordName,soundRecordDescription, soundRecordTag, id, likeSoundScore, reSoundScore}, new int[] {
                            R.id.vers,R.id.name, R.id.api, R.id.hiddenId, R.id.getLikeScore, R.id.getReSoundScore});
                    myOwnPost.setAdapter(adapter);
                    myOwnPost.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(MyFavorites.this, "You Clicked at ", Toast.LENGTH_SHORT).show();
                            String whatIsSoundName = oslist.get(+position).get("soundRecordName");
                            dinleBeni(whatIsSoundName);
                            mybottombar.setVisibility(View.VISIBLE);
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    ///////////////////////////////////////////////

    public void dinleBeni(String whatIsSoundName){
        if (suandaCaliyorsa) {
            playerMy.stop();
            playerMy.reset();
        }
        try {
            Toast.makeText(getApplicationContext(), "http://www.mehmetalp.com.tr/seslitwitter/upload"
                    + whatIsSoundName.toString(), Toast.LENGTH_SHORT).show();
            playerMy.setDataSource("http://www.mehmetalp.com.tr/seslitwitter/upload"
                    + whatIsSoundName.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        playerMy.prepareAsync();
        // playerMy.start();
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_favorites, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
